#pragma once
#include "DataRequest.hpp"

class EmailHandler : public DataRequest<EmailHandler>
{
public:
	EmailHandler()
	{
		request::get("s", m_search);
	}

public:
	bool get_count_sql(sql_string<>& sql)
	{
		if(0 == m_search.length())
		{
			sql.format("select count(*) from t_emails");
		}
		else
		{
			sql.format("select count(*) from t_emails"
					" where (addr like '%%%q%%'"
					" or smtp_ip like '%%%q%%' or pop_ip like '%%%q%%'"
					" or user like '%%%q%%')",
					m_search.c_str(), m_search.c_str(),
					m_search.c_str(), m_search.c_str());
		}

		return true;
	}

	bool get_data_sql(sql_string<>& sql)
	{
		if(0 == m_search.length())
		{
			sql.format("select addr,concat(smtp_ip, ':', smtp_port),"
					" concat(pop_ip, ':', pop_port), user, pwd"
					" from t_emails order by eid");
		}
		else
		{
			sql.format("select addr,concat(smtp_ip, ':', smtp_port),"
					" concat(pop_ip, ':', pop_port), user, pwd"
					" where (addr like '%%%q%%'"
					" or smtp_ip like '%%%q%%' or pop_ip like '%%%q%%'"
					" or user like '%%%q%%')",
					m_search.c_str(), m_search.c_str(),
					m_search.c_str(), m_search.c_str());
		}

		return true;
	}

private:
	std::string m_search;
};

